/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package backend.dbConnection;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

/**
 * class for handling database connection 
 * 
 * @author Markus Moormann
 */
public class ConnectionHelper
{
	/**
	 * a logger to log errors, debug information, etc.
	 */
	private static Logger LOG = Logger.getLogger(ConnectionHelper.class);
	
	public static DataSource ds = null;
	
	/**
	 * get a connection to database
	 * 
	 * @return <code>Connection</code> to database
	 * @throws SQLException if an SQLErrors occurs
	 * @throws ClassNotFoundException 
	 * @throws NamingException 
	 */
	public static Connection getConnection() throws SQLException{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		String urlMySQL = "jdbc:mysql://localhost/flurfunk_development";
		String username = "flurfunk";
		String password = "soutriv6511";
		return DriverManager.getConnection(urlMySQL, username, password);
	}
	
	/**
	 * close the connection
	 * 
	 * @param connection the connection to close
	 */
	public static void close(Connection connection)
	{
		try {
			if (connection != null && !connection.isClosed()) {
				connection.close();
			}
		} catch (SQLException e) {
			LOG.error("Error while closing connection" + e);
		} catch (Exception e)
		{
			LOG.error("Error while closing connection" + e);
		}
	}

	/**
	 * close the statement
	 * 
	 * @param statement the statement to close
	 */
	public static void close(Statement statement) {
		try {
			if(statement != null) {
				statement.close();
			} 
		} catch (SQLException e) {
			LOG.error("Error while closing connection" + e);
		} catch ( Exception e)
		{
			LOG.error("Error while closing connection" + e);
		}

	}
}